using System.Web.Mvc;
using GoldenBicycleCompany.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class ColorsController : Controller
    {
        public ColorsController(IRepository<Color> colorRepository) {
            Check.Require(colorRepository != null, "colorRepository may not be null");

            this.colorRepository = colorRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<Color> colors = colorRepository.GetAll();
            return View(colors);
        }

        [Transaction]
        public ActionResult Show(int id) {
            Color color = colorRepository.Get(id);
            return View(color);
        }

        public ActionResult Create() {
            ColorFormViewModel viewModel = ColorFormViewModel.CreateColorFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Color color) {
            if (ViewData.ModelState.IsValid && color.IsValid()) {
                colorRepository.SaveOrUpdate(color);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The color was successfully created.";
                return RedirectToAction("Index");
            }

            ColorFormViewModel viewModel = ColorFormViewModel.CreateColorFormViewModel();
            viewModel.Color = color;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            ColorFormViewModel viewModel = ColorFormViewModel.CreateColorFormViewModel();
            viewModel.Color = colorRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Color color) {
            Color colorToUpdate = colorRepository.Get(color.Id);
            TransferFormValuesTo(colorToUpdate, color);

            if (ViewData.ModelState.IsValid && color.IsValid()) {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The color was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                colorRepository.DbContext.RollbackTransaction();

				ColorFormViewModel viewModel = ColorFormViewModel.CreateColorFormViewModel();
				viewModel.Color = color;
				return View(viewModel);
            }
        }

        private void TransferFormValuesTo(Color colorToUpdate, Color colorFromForm) {
			colorToUpdate.Name = colorFromForm.Name;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The color was successfully deleted.";
            Color colorToDelete = colorRepository.Get(id);

            if (colorToDelete != null) {
                colorRepository.Delete(colorToDelete);

                try {
                    colorRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the color from being deleted. " +
						"Another item likely depends on this color.";
                    colorRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The color could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the Color form for creates and edits
		/// </summary>
        public class ColorFormViewModel
        {
            private ColorFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static ColorFormViewModel CreateColorFormViewModel() {
                ColorFormViewModel viewModel = new ColorFormViewModel();
                
                return viewModel;
            }

            public Color Color { get; internal set; }
        }

        private readonly IRepository<Color> colorRepository;
    }
}
